package com.webchat.chat.web.socketio;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.model.SocketClient;
import com.webchat.chat.service.SocketClientService;
import com.webchat.chat.web.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/socket/client")
public class SocketClientController extends BaseController {
    @Resource
    private SocketClientService socketClientService;

    @PostMapping("/add")
    public Result add(SocketClient socketClient) {
        socketClientService.save(socketClient);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        socketClientService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SocketClient socketClient) {
        socketClientService.update(socketClient);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        SocketClient socketClient = socketClientService.findById(id);
        return ResultGenerator.genSuccessResult(socketClient);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SocketClient> list = socketClientService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
